package org.dev.biz_order.address

import android.app.AlertDialog
import android.content.DialogInterface
import androidx.fragment.app.FragmentManager
import kotlinx.android.synthetic.main.activity_address_list_item.*
import org.dev.biz_order.R
import org.dev.hi.hiui.item.HiDataItem
import org.dev.hi.hiui.item.HiViewHolder
import org.dev.hi.library.util.HiRes

/**
 * Author WenPing
 * CreateTime 2021/9/4.
 * Description:地址数据Item
 */
class AddressItem(
    var address: Address,
    val fm: FragmentManager,
    val removeItemCallback: (Address, AddressItem) -> Unit,
    val itemClickCallback: (Address) -> Unit,
    val viewModel: AddressViewModel
) : HiDataItem<Address, HiViewHolder>() {
    override fun onBindData(vh: HiViewHolder, position: Int) {
        val context = vh.itemView.context
        vh.user_name.text = address.receiver
        vh.user_phone.text = address.phoneNum
        vh.user_address.text = "${address.province} ${address.city} ${address.area}"
        vh.edit_address.setOnClickListener {
            // 弹出 dialogFragment
            val dialog = AddEditingDialogFragment.newInstance(address)

            dialog.setOnSavedAddressListener(object :
                AddEditingDialogFragment.OnSavedAddressListener {
                override fun onSavedAddress(newAddress: Address) {
                    address = newAddress
                    // 保存地址和更新地址信息后刷新数据
                    refreshItem()
                }
            })
            dialog.show(fm, "edit_address")
        }
        vh.delete.setOnClickListener {
            AlertDialog.Builder(context)
                .setMessage(HiRes.getString(R.string.address_delete_title))
                .setNegativeButton(R.string.address_delete_cancel, null)
                .setPositiveButton(
                    R.string.address_delete_ensure
                ) { dialog, which ->
                    // 点击确认按钮,dismiss dialogFragment
                    dialog.dismiss()
                    removeItemCallback(address, this)
                }.show()
        }
        vh.itemView.setOnClickListener {
            itemClickCallback(address)
        }
        vh.default_address.setOnClickListener {
            viewModel.apply {
                checkedPosition = position
                checkedAddressItem?.refreshItem()
                checkedAddressItem = this@AddressItem
                // 刷新 选中项
                refreshItem()
            }
        }
        val select = viewModel.checkedAddressItem == this &&
                viewModel.checkedPosition == position
        vh.default_address.setTextColor(
            HiRes.getColor(if (select) R.color.color_dd2 else R.color.color_999)
        )
        vh.default_address.text = HiRes.getString(
            if (select) R.string.address_default else R.string.set_default_address
        )
        // 被选中时显示左侧的drawable
        vh.default_address.setCompoundDrawablesWithIntrinsicBounds(
            if (select) R.drawable.ic_checked_red else 0,
            0,
            0,
            0
        )
    }

    override fun getItemLayoutRes(): Int = R.layout.activity_address_list_item


}